<?php
/**
 * @title 1599. 经营摩天轮的最大利润
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $customers
     * @param Integer $boardingCost
     * @param Integer $runningCost
     * @return Integer
     */
    function minOperationsMaxProfit($customers, $boardingCost, $runningCost) {
        /**
         * @since 2024-01-01 最大利润，最少转动次数
         */
        $maxProfit = 0;
        $minRotationCount = -1;

        /**
         * @since 2024-01-01 4个旅客都不赚钱
         */
        if(($boardingCost*4) <= $runningCost){
            return -1;
        } else {}

        /**
         * @since 2024-01-01 等待旅客数量
         */
        $waitCustomerCount = 0;

        /**
         * @since 2024-01-01 累计利润
         */
        $profit = 0;

        /**
         * @since 2024-01-01 迭代游客数组
         */
        for($i=0; $i<count($customers); $i++){
            /**
             * @since 2024-01-01 当前旅客数量
             */
            $customerCount = $customers[$i] + $waitCustomerCount;

            /**
             * @since 2024-01-01 本次转动的利润
             */
            $profit += min(4, $customerCount)*$boardingCost - $runningCost;

            /**
             * @since 2024-01-01 发现更大利润
             */
            if($profit > $maxProfit){
                /**
                 * @since 2024-01-01 更新转动次数
                 */
                $minRotationCount = $i+1;
                $maxProfit = $profit;
            } else {}

            /**
             * @since 2024-01-01 更新等待旅客数量
             */
            $waitCustomerCount = max(0, $customerCount-4);
        }

        /**
         * @since 2024-01-01 还有等待旅客
         */
        if($waitCustomerCount > 0){
            /**
             * @since 2024-01-01 先处理4个一组
             */
            $n = intval($waitCustomerCount/4);

            /**
             * @since 2024-01-01 更新利润
             */
            $profit += ($boardingCost*4 - $runningCost)*$n;
            $i = $i+$n;
            if($profit > $maxProfit){
                $minRotationCount = $i;
                $maxProfit = $profit;
            } else {}

            /**
             * @since 2024-01-01 还有剩余旅客
             */
            $waitCustomerCount = $waitCustomerCount % 4;
            if($waitCustomerCount > 0){
                /**
                 * @since 2024-01-01 更新利润，转动次数
                 */
                $profit += $boardingCost*$waitCustomerCount - $runningCost;
                $i++;
                if($profit > $maxProfit){
                    $minRotationCount = $i;
                    $maxProfit = $profit;
                } else {}
            } else {}
        } else {}

        /**
         * @return Integer
         */
        return $minRotationCount;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.01.php
 */
$datas = [
    [[8,3], 5, 6], // 3
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minOperationsMaxProfit(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
